<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>中文文件名测试</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .test-section {
            margin: 20px 0;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .test-section h3 {
            color: #333;
            margin-top: 0;
        }
        input[type="file"] {
            margin: 10px 0;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #0056b3;
        }
        .result {
            margin: 10px 0;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
            border-left: 4px solid #007bff;
        }
        .success {
            border-left-color: #28a745;
            background: #d4edda;
        }
        .error {
            border-left-color: #dc3545;
            background: #f8d7da;
        }
        .file-list {
            margin: 10px 0;
        }
        .file-item {
            padding: 10px;
            border: 1px solid #ddd;
            margin: 5px 0;
            border-radius: 5px;
            background: #f9f9f9;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🧪 中文文件名测试页面</h1>
        <p>此页面用于测试文件存储服务的中文文件名处理功能。</p>

        <div class="test-section">
            <h3>📤 文件上传测试</h3>
            <p>请选择一个包含中文文件名的文件进行上传测试：</p>
            <input type="file" id="testFile" accept="*/*">
            <button onclick="uploadTestFile()">上传文件</button>
            <div id="uploadResult"></div>
        </div>

        <div class="test-section">
            <h3>📋 文件列表测试</h3>
            <button onclick="loadFileList()">获取文件列表</button>
            <div id="fileListResult"></div>
        </div>

        <div class="test-section">
            <h3>📊 统计信息测试</h3>
            <button onclick="loadStats()">获取统计信息</button>
            <div id="statsResult"></div>
        </div>

        <div class="test-section">
            <h3>🔧 快速测试</h3>
            <button onclick="runAllTests()">运行所有测试</button>
            <div id="allTestsResult"></div>
        </div>
    </div>

    <script>
        // 上传测试文件
        async function uploadTestFile() {
            const fileInput = document.getElementById('testFile');
            const resultDiv = document.getElementById('uploadResult');
            
            if (!fileInput.files[0]) {
                resultDiv.innerHTML = '<div class="result error">请先选择文件</div>';
                return;
            }

            const file = fileInput.files[0];
            const formData = new FormData();
            formData.append('file', file);

            try {
                resultDiv.innerHTML = '<div class="result">上传中...</div>';
                
                const response = await fetch('/api/upload', {
                    method: 'POST',
                    body: formData
                });

                const result = await response.json();
                
                if (result.success) {
                    resultDiv.innerHTML = `
                        <div class="result success">
                            <h4>✅ 上传成功！</h4>
                            <p><strong>原始文件名:</strong> ${result.file.originalName}</p>
                            <p><strong>存储文件名:</strong> ${result.file.name}</p>
                            <p><strong>文件大小:</strong> ${formatFileSize(result.file.size)}</p>
                            <p><strong>下载链接:</strong> <a href="${result.file.url}" target="_blank">下载文件</a></p>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `<div class="result error">❌ 上传失败: ${result.error}</div>`;
                }
            } catch (error) {
                resultDiv.innerHTML = `<div class="result error">❌ 请求失败: ${error.message}</div>`;
            }
        }

        // 获取文件列表
        async function loadFileList() {
            const resultDiv = document.getElementById('fileListResult');
            
            try {
                resultDiv.innerHTML = '<div class="result">加载中...</div>';
                
                const response = await fetch('/api/files');
                const result = await response.json();
                
                if (result.success) {
                    if (result.files.length === 0) {
                        resultDiv.innerHTML = '<div class="result">暂无文件</div>';
                    } else {
                        const fileListHtml = result.files.map(file => `
                            <div class="file-item">
                                <p><strong>文件名:</strong> ${file.name}</p>
                                <p><strong>大小:</strong> ${formatFileSize(file.size)}</p>
                                <p><strong>上传时间:</strong> ${new Date(file.uploadTime).toLocaleString()}</p>
                                <p><strong>操作:</strong> <a href="${file.url}" target="_blank">下载</a> | 
                                <button onclick="deleteFile('${file.name}')" style="background: #dc3545; padding: 2px 8px; font-size: 12px;">删除</button></p>
                            </div>
                        `).join('');
                        
                        resultDiv.innerHTML = `
                            <div class="result success">
                                <h4>📋 文件列表 (共 ${result.files.length} 个文件)</h4>
                                <div class="file-list">${fileListHtml}</div>
                            </div>
                        `;
                    }
                } else {
                    resultDiv.innerHTML = `<div class="result error">❌ 获取文件列表失败: ${result.error}</div>`;
                }
            } catch (error) {
                resultDiv.innerHTML = `<div class="result error">❌ 请求失败: ${error.message}</div>`;
            }
        }

        // 获取统计信息
        async function loadStats() {
            const resultDiv = document.getElementById('statsResult');
            
            try {
                resultDiv.innerHTML = '<div class="result">加载中...</div>';
                
                const response = await fetch('/api/stats');
                const result = await response.json();
                
                if (result.success) {
                    resultDiv.innerHTML = `
                        <div class="result success">
                            <h4>📊 统计信息</h4>
                            <p><strong>文件总数:</strong> ${result.stats.fileCount}</p>
                            <p><strong>总存储大小:</strong> ${result.stats.totalSizeMB} MB</p>
                            <p><strong>总字节数:</strong> ${result.stats.totalSize.toLocaleString()} bytes</p>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `<div class="result error">❌ 获取统计信息失败: ${result.error}</div>`;
                }
            } catch (error) {
                resultDiv.innerHTML = `<div class="result error">❌ 请求失败: ${error.message}</div>`;
            }
        }

        // 删除文件
        async function deleteFile(filename) {
            if (!confirm(`确定要删除文件 "${filename}" 吗？`)) {
                return;
            }

            try {
                const response = await fetch(`/api/files/${encodeURIComponent(filename)}`, {
                    method: 'DELETE'
                });

                const result = await response.json();
                
                if (result.success) {
                    alert('文件删除成功！');
                    loadFileList(); // 重新加载文件列表
                } else {
                    alert(`删除失败: ${result.error}`);
                }
            } catch (error) {
                alert(`删除请求失败: ${error.message}`);
            }
        }

        // 运行所有测试
        async function runAllTests() {
            const resultDiv = document.getElementById('allTestsResult');
            resultDiv.innerHTML = '<div class="result">🧪 开始运行所有测试...</div>';
            
            const results = [];
            
            // 测试1: 获取统计信息
            try {
                const statsResponse = await fetch('/api/stats');
                const statsResult = await statsResponse.json();
                results.push(`📊 统计信息测试: ${statsResult.success ? '✅ 成功' : '❌ 失败'}`);
            } catch (error) {
                results.push(`📊 统计信息测试: ❌ 失败 - ${error.message}`);
            }
            
            // 测试2: 获取文件列表
            try {
                const filesResponse = await fetch('/api/files');
                const filesResult = await filesResponse.json();
                results.push(`📋 文件列表测试: ${filesResult.success ? '✅ 成功' : '❌ 失败'}`);
            } catch (error) {
                results.push(`📋 文件列表测试: ❌ 失败 - ${error.message}`);
            }
            
            // 显示测试结果
            resultDiv.innerHTML = `
                <div class="result success">
                    <h4>🧪 测试结果</h4>
                    ${results.map(result => `<p>${result}</p>`).join('')}
                    <p><strong>💡 提示:</strong> 请上传一个包含中文文件名的文件来测试完整功能。</p>
                </div>
            `;
        }

        // 格式化文件大小
        function formatFileSize(bytes) {
            if (bytes === 0) return '0 Bytes';
            const k = 1024;
            const sizes = ['Bytes', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }

        // 页面加载时自动运行基础测试
        window.onload = function() {
            loadStats();
        };
    </script>
</body>
</html>
